﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace AS3___CG
{
    public class InputManager
    {
        private const float MOVEMENTSPEED = 0.01f;
        private const float ROTATIONSPEED = 0.001f;
        private Game _game;
        private Camera _camera;
        private Quaternion rotation;
        

        public InputManager(Game game, Camera camera)
        {
            _game = game;
            _camera = camera;
        }

        public void getAction(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                _game.Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                rotation = Quaternion.CreateFromYawPitchRoll(0f, -ROTATIONSPEED*gameTime.ElapsedGameTime.Milliseconds, 0f);
                _camera.cPos = Vector3.Transform(_camera.cPos, rotation); 
            }
            
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                rotation = Quaternion.CreateFromYawPitchRoll(0f, ROTATIONSPEED * gameTime.ElapsedGameTime.Milliseconds, 0f);
                _camera.cPos = Vector3.Transform(_camera.cPos, rotation); 
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                rotation = Quaternion.CreateFromYawPitchRoll(ROTATIONSPEED * gameTime.ElapsedGameTime.Milliseconds, 0f, 0f);
                _camera.cPos = Vector3.Transform(_camera.cPos, rotation); 
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                rotation = Quaternion.CreateFromYawPitchRoll(-ROTATIONSPEED * gameTime.ElapsedGameTime.Milliseconds, 0f, 0f);
                _camera.cPos = Vector3.Transform(_camera.cPos, rotation); 
            }

            if (Keyboard.GetState().IsKeyDown(Keys.A))
            {

               // _camera.cTar.Y += 0.01f * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                _camera.cPos.Y += 0.01f * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Z))
            {
              //  _camera.cTar.Y -= 0.01f * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                _camera.cPos.Y -= 0.01f * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {


                Vector3 normCamPos;
                normCamPos = _camera.cPos;
                normCamPos.Normalize();
                _camera.cPos -= normCamPos * MOVEMENTSPEED * gameTime.ElapsedGameTime.Milliseconds;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.S))
            {

                Vector3 temp;
                temp = _camera.cPos;
                temp.Normalize();
                _camera.cPos += temp * MOVEMENTSPEED * gameTime.ElapsedGameTime.Milliseconds;
            }
        }
    }
}
